#include "login.h"

Login::Login(QWidget *parent)
    : QWidget(parent)
{
    initDatabase();
    setupUI();
}

Login::~Login()
{
//    if (db.isOpen()) {
//            db.close();
//        }
}

bool Login::initDatabase()
{
    db = QSqlDatabase::addDatabase("QODBC");
    db.setHostName("120.26.201.192");
    db.setPort(3306);
    db.setDatabaseName("3");
    db.setUserName("root");
    db.setPassword("666888");
    bool ok = db.open();
    if (ok){
        //QMessageBox::information(this, "infor", "success");
        return true;
    }
    else {
       // QMessageBox::information(this, "infor", "open failed");
        qDebug()<<"error open database because"<<db.lastError().text();
        return false;
    }
}

void Login::setupUI()
{
    setWindowTitle("智能农场");
    setWindowIcon(QIcon(":/icon/farm2.png"));
    setFixedSize(400, 300);

    // 创建控件
    titleLabel = new QLabel("智能农场管理系统", this);
    titleLabel->setAlignment(Qt::AlignCenter);
    QFont titleFont = titleLabel->font();
    titleFont.setPointSize(20);
    titleFont.setBold(true);
    titleLabel->setFont(titleFont);
    titleLabel->setStyleSheet("color: #2c6e49;");

    usernameEdit = new QLineEdit(this);
    usernameEdit->setMinimumHeight(35);
    usernameEdit->setStyleSheet("QLineEdit { padding: 5px; border: 1px solid #ccc; border-radius: 4px; }");

    passwordEdit = new QLineEdit(this);
    passwordEdit->setMinimumHeight(35);
    passwordEdit->setEchoMode(QLineEdit::Password);
    passwordEdit->setStyleSheet("QLineEdit { padding: 5px; border: 1px solid #ccc; border-radius: 4px; }");

    loginBtn = new QPushButton("登 录", this);
    loginBtn->setMinimumHeight(40);
    loginBtn->setStyleSheet("QPushButton { background-color: #4b8a29; color: white; border-radius: 4px; font-weight: bold; }"
                         "QPushButton:hover { background-color: #3e7321; }");

    // 主布局
    QVBoxLayout *mainLayout = new QVBoxLayout(this);
    mainLayout->setContentsMargins(30, 30, 30, 30);
    mainLayout->setSpacing(20);

    // 标题布局
    mainLayout->addWidget(titleLabel);
    mainLayout->addSpacing(20);  // 替代原来的addStretch(1)

    // 表单布局
    QFormLayout *formLayout = new QFormLayout();
    formLayout->setLabelAlignment(Qt::AlignRight);
    formLayout->setFormAlignment(Qt::AlignHCenter | Qt::AlignTop);
    formLayout->setSpacing(15);

    QFont labelFont;
    labelFont.setPointSize(12);

    QLabel *userLabel = new QLabel("用户名:", this);
    userLabel->setFont(labelFont);
    userLabel->setStyleSheet("color: #444;");
    formLayout->addRow(userLabel, usernameEdit);

    QLabel *pwdLabel = new QLabel("密 码:", this);
    pwdLabel->setFont(labelFont);
    pwdLabel->setStyleSheet("color: #444;");
    formLayout->addRow(pwdLabel, passwordEdit);

    mainLayout->addLayout(formLayout);
    mainLayout->addSpacing(20);  // 表单和按钮之间的间距

    // 按钮布局
    QHBoxLayout *btnLayout = new QHBoxLayout();
    btnLayout->addStretch();
    btnLayout->addWidget(loginBtn);
    btnLayout->addStretch();
    loginBtn->setFixedWidth(120);

    mainLayout->addLayout(btnLayout);
    mainLayout->addSpacing(10);  // 底部间距，比之前更小

    connect(loginBtn, &QPushButton::clicked, this, &Login::LoginClicked);

}

void Login::LoginClicked()
{
    if (usernameEdit->text().isEmpty()) {
            QMessageBox::warning(this, "输入错误", "用户名不能为空");
            usernameEdit->setFocus();
            return;
        }
    if (passwordEdit->text().isEmpty()) {
            QMessageBox::warning(this, "输入错误", "密码不能为空");
            passwordEdit->setFocus();
            return;
        }
    if (!db.isOpen()) {
                QMessageBox::critical(this, "数据库错误", "数据库连接已断开");
                return;
        }
       QString username = usernameEdit->text();
       QString password = passwordEdit->text();
    QSqlQuery query;
    QString sql = QString("SELECT * FROM user WHERE admin = %1 AND passwd = '%2'")
                  .arg(username)
                  .arg(password); // 注意密码要加引号

    if(!query.exec(sql)) {
        QMessageBox::warning(this, "错误", "查询失败: " + query.lastError().text());
        return;
    }

    if(query.next()) {

        //QMessageBox::warning(this, "登录成功", "zhengque");
        QString permission = query.value(2).toString();
        QString username = usernameEdit->text();
                bool isAdmin = (username == "1"); // 判断是否为管理员账号
        emit loginSuccess(isAdmin);
        emit loginSuccess2(permission);
            //list->show();
        }

    else {
        QMessageBox::warning(this, "登录失败", "账号或密码错误");
        passwordEdit->clear();
        passwordEdit->setFocus();

}
}

